#include <iostream>
using namespace std;
#include <fstream>
#include <cstring>
#define BUFFER_SIZE 128

int main()
{
#if 0
    /*向文件中写数据*/
    ofstream ofs;
    string filename = "./record.txt";
    ofs.open(filename, ios::out);

    /*判断文件是否打开成功 */
    if (ofs.is_open() == false)
    {
        std::cout << "失败" << endl;
        exit(-1);
    }

    const char *data = "2025恭喜发财!";
    /*向文件中写数据*/
    ofs.write(data, strlen(data));

    const char *data2 = "2025发大财。";
    /*再次向文件中写入数据 */
    ofs << data2 << endl;

    ofs.close();
#endif
    /*从文件中读数据*/
    ifstream ifs;
    string filename = "./record.txt";
    ifs.open(filename, ios::in);

    if (ifs.is_open() == false)
    {
        cout << "file open error" << endl;
        exit(-1);
    }

    char buffer[BUFFER_SIZE] = {0};
    while (ifs.eof() == false)
    {
        // /*读取数据*/
        // ifs.read(buffer, sizeof(buffer));
        // cout << "buffer:" << buffer << endl;
        // memset(buffer,0,sizeof(buffer));

#if 0 // 读取数据
        ifs.getline(buffer,sizeof(buffer)-1);
        cout << "buffer:"<< buffer<< endl;
        memset(buffer,0,sizeof(buffer));
#endif
#if 1
        /*从文件中读取数据，遇到空格或者换行就结束*/
        ifs >> buffer;
        cout << "buffer:" << buffer << endl;
#endif
    }
    ifs.close();
    return 0;
}